package cst.neu.yuejian_31.model;

import cst.neu.yuejian_31.bean.MatchCollectBean;
import cst.neu.yuejian_31.iface.MatchCollectIface;
import cst.neu.yuejian_31.iface.MatchCollectListener;
import cst.neu.yuejian_31.Common.Constant;
import cst.neu.yuejian_31.service.MatchCollectService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by Alienware on 2017/10/29.
 */

public class MatchCollectMdoel implements MatchCollectIface {
    private Retrofit retrofit;
    public MatchCollectMdoel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Constant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    @Override
    public void matchCollect(String userId, String matchId, final MatchCollectListener matchCollectListener) {
        MatchCollectService matchCollectService=retrofit.create(MatchCollectService.class);
        Call<MatchCollectBean> call=
                matchCollectService.matchCollect(userId,matchId);
        call.enqueue(new Callback<MatchCollectBean>() {
            @Override
            public void onResponse(Call<MatchCollectBean> call, Response<MatchCollectBean> response) {
                if(response.isSuccessful()&&response.body()!=null){
                    matchCollectListener.onSuccess(response.body());
                }else {
                    matchCollectListener.onFail("Fail");
                }
            }

            @Override
            public void onFailure(Call<MatchCollectBean> call, Throwable t) {
                matchCollectListener.onFail(t.toString());
            }
        });
    }
}
